

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>O365.utils.attachment &mdash; O365  documentation</title>
  

  
  
  
  

  

  
  
    

  

  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 

  
  <script src="../../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../../index.html" class="icon icon-home"> O365
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../getting_started.html">Getting Started</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../usage.html">Detailed Usage</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../api.html">O365 API</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">O365</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>O365.utils.attachment</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for O365.utils.attachment</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">base64</span>
<span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">from</span> <span class="nn">pathlib</span> <span class="kn">import</span> <span class="n">Path</span>
<span class="kn">from</span> <span class="nn">io</span> <span class="kn">import</span> <span class="n">BytesIO</span>

<span class="kn">from</span> <span class="nn">.utils</span> <span class="kn">import</span> <span class="n">ApiComponent</span>

<span class="n">log</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>


<div class="viewcode-block" id="AttachableMixin"><a class="viewcode-back" href="../../../api/attachment.html#O365.utils.attachment.AttachableMixin">[docs]</a><span class="k">class</span> <span class="nc">AttachableMixin</span><span class="p">:</span>
<div class="viewcode-block" id="AttachableMixin.__init__"><a class="viewcode-back" href="../../../api/attachment.html#O365.utils.attachment.AttachableMixin.__init__">[docs]</a>    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attachment_name_property</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">attachment_type</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; Defines the functionality for an object to be attachable.</span>
<span class="sd">        Any object that inherits from this class will be attachable</span>
<span class="sd">        (if the underlying api allows that)</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__attachment_name</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__attachment_name_property</span> <span class="o">=</span> <span class="n">attachment_name_property</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__attachment_type</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_gk</span><span class="p">(</span><span class="n">attachment_type</span><span class="p">)</span></div>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">attachment_name</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; Name of the attachment</span>

<span class="sd">        :getter: get attachment name</span>
<span class="sd">        :setter: set new name for the attachment</span>
<span class="sd">        :type: str</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__attachment_name</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__attachment_name</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__attachment_name_property</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">__attachment_name_property</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># property order resolution:</span>
            <span class="c1"># 1) try property &#39;subject&#39;</span>
            <span class="c1"># 2) try property &#39;name&#39;</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">attachment_name</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">&#39;subject&#39;</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
                <span class="n">attachment_name</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">attachment_name</span>

    <span class="nd">@attachment_name</span><span class="o">.</span><span class="n">setter</span>
    <span class="k">def</span> <span class="nf">attachment_name</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__attachment_name</span> <span class="o">=</span> <span class="n">value</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">attachment_type</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; Type of attachment</span>

<span class="sd">        :rtype: str</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__attachment_type</span>

<div class="viewcode-block" id="AttachableMixin.to_api_data"><a class="viewcode-back" href="../../../api/attachment.html#O365.utils.attachment.AttachableMixin.to_api_data">[docs]</a>    <span class="k">def</span> <span class="nf">to_api_data</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; Returns a dict to communicate with the server</span>

<span class="sd">        :rtype: dict</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">()</span></div></div>


<div class="viewcode-block" id="BaseAttachment"><a class="viewcode-back" href="../../../api/attachment.html#O365.utils.attachment.BaseAttachment">[docs]</a><span class="k">class</span> <span class="nc">BaseAttachment</span><span class="p">(</span><span class="n">ApiComponent</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; BaseAttachment class is the base object for dealing with attachments &quot;&quot;&quot;</span>

    <span class="n">_endpoints</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;attach&#39;</span><span class="p">:</span> <span class="s1">&#39;/messages/</span><span class="si">{id}</span><span class="s1">/attachments&#39;</span><span class="p">}</span>

<div class="viewcode-block" id="BaseAttachment.__init__"><a class="viewcode-back" href="../../../api/attachment.html#O365.utils.attachment.BaseAttachment.__init__">[docs]</a>    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attachment</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">*</span><span class="p">,</span> <span class="n">parent</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; Creates a new attachment, optionally from existing cloud data</span>

<span class="sd">        :param attachment: attachment data (dict = cloud data,</span>
<span class="sd">         other = user data)</span>
<span class="sd">        :type attachment: dict or str or Path or list[str] or AttachableMixin</span>
<span class="sd">        :param BaseAttachments parent: the parent Attachments</span>
<span class="sd">        :param Protocol protocol: protocol to use if no parent specified</span>
<span class="sd">         (kwargs)</span>
<span class="sd">        :param str main_resource: use this resource instead of parent resource</span>
<span class="sd">         (kwargs)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">kwargs</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="s1">&#39;protocol&#39;</span><span class="p">,</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="s1">&#39;protocol&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">))</span>
        <span class="n">kwargs</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="s1">&#39;main_resource&#39;</span><span class="p">,</span>
                          <span class="nb">getattr</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="s1">&#39;main_resource&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">))</span>

        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">attachment_type</span> <span class="o">=</span> <span class="s1">&#39;file&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">attachment_id</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">content_id</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">is_inline</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">attachment</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">content</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">on_disk</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">on_cloud</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;on_cloud&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">size</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="k">if</span> <span class="n">attachment</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">attachment</span><span class="p">,</span> <span class="nb">dict</span><span class="p">):</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cloud_data_key</span> <span class="ow">in</span> <span class="n">attachment</span><span class="p">:</span>
                    <span class="c1"># data from the cloud</span>
                    <span class="n">attachment</span> <span class="o">=</span> <span class="n">attachment</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_cloud_data_key</span><span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">attachment_id</span> <span class="o">=</span> <span class="n">attachment</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_cc</span><span class="p">(</span><span class="s1">&#39;id&#39;</span><span class="p">),</span> <span class="kc">None</span><span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">content_id</span> <span class="o">=</span> <span class="n">attachment</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_cc</span><span class="p">(</span><span class="s1">&#39;contentId&#39;</span><span class="p">),</span> <span class="kc">None</span><span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">is_inline</span> <span class="o">=</span> <span class="n">attachment</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_cc</span><span class="p">(</span><span class="s1">&#39;IsInline&#39;</span><span class="p">),</span> <span class="kc">False</span><span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">attachment</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_cc</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">),</span> <span class="kc">None</span><span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">content</span> <span class="o">=</span> <span class="n">attachment</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_cc</span><span class="p">(</span><span class="s1">&#39;contentBytes&#39;</span><span class="p">),</span>
                                                  <span class="kc">None</span><span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">attachment_type</span> <span class="o">=</span> <span class="s1">&#39;item&#39;</span> <span class="k">if</span> <span class="s1">&#39;item&#39;</span> <span class="ow">in</span> <span class="n">attachment</span><span class="o">.</span><span class="n">get</span><span class="p">(</span>
                        <span class="s1">&#39;@odata.type&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="k">else</span> <span class="s1">&#39;file&#39;</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">on_disk</span> <span class="o">=</span> <span class="kc">False</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">size</span> <span class="o">=</span> <span class="n">attachment</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_cc</span><span class="p">(</span><span class="s1">&#39;size&#39;</span><span class="p">),</span> <span class="kc">None</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">file_path</span> <span class="o">=</span> <span class="n">attachment</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;path&#39;</span><span class="p">,</span> <span class="n">attachment</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">))</span>
                    <span class="k">if</span> <span class="n">file_path</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Must provide a valid &quot;path&quot; or &#39;</span>
                                         <span class="s1">&#39;&quot;name&quot; for the attachment&#39;</span><span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">content</span> <span class="o">=</span> <span class="n">attachment</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;content&#39;</span><span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">on_disk</span> <span class="o">=</span> <span class="n">attachment</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;on_disk&#39;</span><span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">attachment_id</span> <span class="o">=</span> <span class="n">attachment</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;attachment_id&#39;</span><span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">attachment</span> <span class="o">=</span> <span class="n">Path</span><span class="p">(</span><span class="n">file_path</span><span class="p">)</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">on_disk</span> <span class="k">else</span> <span class="kc">None</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">attachment</span><span class="o">.</span><span class="n">name</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">on_disk</span>
                                 <span class="k">else</span> <span class="n">attachment</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">))</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">size</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">attachment</span><span class="o">.</span><span class="n">stat</span><span class="p">()</span><span class="o">.</span><span class="n">st_size</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">attachment</span> <span class="k">else</span> <span class="kc">None</span>

            <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">attachment</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">attachment</span> <span class="o">=</span> <span class="n">Path</span><span class="p">(</span><span class="n">attachment</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">attachment</span><span class="o">.</span><span class="n">name</span>
            <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">attachment</span><span class="p">,</span> <span class="n">Path</span><span class="p">):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">attachment</span> <span class="o">=</span> <span class="n">attachment</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">attachment</span><span class="o">.</span><span class="n">name</span>
            <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">attachment</span><span class="p">,</span> <span class="p">(</span><span class="nb">tuple</span><span class="p">,</span> <span class="nb">list</span><span class="p">)):</span>
                <span class="c1"># files with custom names or Inmemory objects</span>
                <span class="n">file_obj</span><span class="p">,</span> <span class="n">custom_name</span> <span class="o">=</span> <span class="n">attachment</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">file_obj</span><span class="p">,</span> <span class="n">BytesIO</span><span class="p">):</span>
                    <span class="c1"># in memory objects</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">content</span> <span class="o">=</span> <span class="n">base64</span><span class="o">.</span><span class="n">b64encode</span><span class="p">(</span><span class="n">file_obj</span><span class="o">.</span><span class="n">getvalue</span><span class="p">())</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">attachment</span> <span class="o">=</span> <span class="n">Path</span><span class="p">(</span><span class="n">file_obj</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">custom_name</span>

            <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">attachment</span><span class="p">,</span> <span class="n">AttachableMixin</span><span class="p">):</span>
                <span class="c1"># Object that can be attached (Message for example)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">attachment_type</span> <span class="o">=</span> <span class="s1">&#39;item&#39;</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">attachment</span> <span class="o">=</span> <span class="n">attachment</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">attachment</span><span class="o">.</span><span class="n">attachment_name</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">content</span> <span class="o">=</span> <span class="n">attachment</span><span class="o">.</span><span class="n">to_api_data</span><span class="p">()</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">content</span><span class="p">[</span><span class="s1">&#39;@odata.type&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">attachment</span><span class="o">.</span><span class="n">attachment_type</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">content</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">attachment</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">attachment</span><span class="o">.</span><span class="n">exists</span><span class="p">():</span>
                <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">attachment</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s1">&#39;rb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">file</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">content</span> <span class="o">=</span> <span class="n">base64</span><span class="o">.</span><span class="n">b64encode</span><span class="p">(</span><span class="n">file</span><span class="o">.</span><span class="n">read</span><span class="p">())</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">on_disk</span> <span class="o">=</span> <span class="kc">True</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">size</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">attachment</span><span class="o">.</span><span class="n">stat</span><span class="p">()</span><span class="o">.</span><span class="n">st_size</span></div>

    <span class="k">def</span> <span class="fm">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; Returns the size of this attachment &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">size</span>

    <span class="k">def</span> <span class="fm">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">attachment_id</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">attachment_id</span>

<div class="viewcode-block" id="BaseAttachment.to_api_data"><a class="viewcode-back" href="../../../api/attachment.html#O365.utils.attachment.BaseAttachment.to_api_data">[docs]</a>    <span class="k">def</span> <span class="nf">to_api_data</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; Returns a dict to communicate with the server</span>

<span class="sd">        :rtype: dict</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">data</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;@odata.type&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_gk</span><span class="p">(</span>
            <span class="s1">&#39;</span><span class="si">{}</span><span class="s1">_attachment_type&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">attachment_type</span><span class="p">)),</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_cc</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">):</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">}</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_inline</span><span class="p">:</span>
            <span class="n">data</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">_cc</span><span class="p">(</span><span class="s1">&#39;isInline&#39;</span><span class="p">)]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_inline</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">attachment_type</span> <span class="o">==</span> <span class="s1">&#39;file&#39;</span><span class="p">:</span>
            <span class="n">data</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">_cc</span><span class="p">(</span><span class="s1">&#39;contentBytes&#39;</span><span class="p">)]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">content</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">content_id</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">data</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">_cc</span><span class="p">(</span><span class="s1">&#39;contentId&#39;</span><span class="p">)]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">content_id</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">data</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">_cc</span><span class="p">(</span><span class="s1">&#39;item&#39;</span><span class="p">)]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">content</span>

        <span class="k">return</span> <span class="n">data</span></div>

<div class="viewcode-block" id="BaseAttachment.save"><a class="viewcode-back" href="../../../api/attachment.html#O365.utils.attachment.BaseAttachment.save">[docs]</a>    <span class="k">def</span> <span class="nf">save</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">location</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">custom_name</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;  Save the attachment locally to disk</span>

<span class="sd">        :param str location: path string to where the file is to be saved.</span>
<span class="sd">        :param str custom_name: a custom name to be saved as</span>
<span class="sd">        :return: Success / Failure</span>
<span class="sd">        :rtype: bool</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">content</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">False</span>

        <span class="n">location</span> <span class="o">=</span> <span class="n">Path</span><span class="p">(</span><span class="n">location</span> <span class="ow">or</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">location</span><span class="o">.</span><span class="n">exists</span><span class="p">():</span>
            <span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;the location provided does not exist&#39;</span><span class="p">)</span>
            <span class="k">return</span> <span class="kc">False</span>

        <span class="n">name</span> <span class="o">=</span> <span class="n">custom_name</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span>
        <span class="n">name</span> <span class="o">=</span> <span class="n">name</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="s1">&#39;-&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">location</span> <span class="o">/</span> <span class="n">name</span>
            <span class="k">with</span> <span class="n">path</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s1">&#39;wb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">file</span><span class="p">:</span>
                <span class="n">file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">base64</span><span class="o">.</span><span class="n">b64decode</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">content</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">attachment</span> <span class="o">=</span> <span class="n">path</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">on_disk</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">size</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">attachment</span><span class="o">.</span><span class="n">stat</span><span class="p">()</span><span class="o">.</span><span class="n">st_size</span>

            <span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;file saved locally.&#39;</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="n">log</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s1">&#39;file failed to be saved: </span><span class="si">%s</span><span class="s1">&#39;</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="p">))</span>
            <span class="k">return</span> <span class="kc">False</span>
        <span class="k">return</span> <span class="kc">True</span></div>

<div class="viewcode-block" id="BaseAttachment.attach"><a class="viewcode-back" href="../../../api/attachment.html#O365.utils.attachment.BaseAttachment.attach">[docs]</a>    <span class="k">def</span> <span class="nf">attach</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">api_object</span><span class="p">,</span> <span class="n">on_cloud</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; Attach this attachment to an existing api_object. This</span>
<span class="sd">        BaseAttachment object must be an orphan BaseAttachment created for the</span>
<span class="sd">        sole purpose of attach it to something and therefore run this method.</span>

<span class="sd">        :param api_object: object to attach to</span>
<span class="sd">        :param on_cloud: if the attachment is on cloud or not</span>
<span class="sd">        :return: Success / Failure</span>
<span class="sd">        :rtype: bool</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">on_cloud</span><span class="p">:</span>
            <span class="c1"># item is already saved on the cloud.</span>
            <span class="k">return</span> <span class="kc">True</span>

        <span class="c1"># api_object must exist and if implements attachments</span>
        <span class="c1"># then we can attach to it.</span>
        <span class="k">if</span> <span class="n">api_object</span> <span class="ow">and</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">api_object</span><span class="p">,</span> <span class="s1">&#39;attachments&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">on_cloud</span><span class="p">:</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">api_object</span><span class="o">.</span><span class="n">object_id</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span>
                        <span class="s1">&#39;A valid object id is needed in order to attach a file&#39;</span><span class="p">)</span>
                <span class="c1"># api_object builds its own url using its</span>
                <span class="c1"># resource and main configuration</span>
                <span class="n">url</span> <span class="o">=</span> <span class="n">api_object</span><span class="o">.</span><span class="n">build_url</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_endpoints</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;attach&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                    <span class="nb">id</span><span class="o">=</span><span class="n">api_object</span><span class="o">.</span><span class="n">object_id</span><span class="p">))</span>

                <span class="n">response</span> <span class="o">=</span> <span class="n">api_object</span><span class="o">.</span><span class="n">con</span><span class="o">.</span><span class="n">post</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">to_api_data</span><span class="p">())</span>

                <span class="k">return</span> <span class="nb">bool</span><span class="p">(</span><span class="n">response</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">attachment_type</span> <span class="o">==</span> <span class="s1">&#39;file&#39;</span><span class="p">:</span>
                    <span class="n">api_object</span><span class="o">.</span><span class="n">attachments</span><span class="o">.</span><span class="n">add</span><span class="p">([{</span>
                        <span class="s1">&#39;attachment_id&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">attachment_id</span><span class="p">,</span>
                        <span class="c1"># TODO: copy attachment id? or set to None?</span>
                        <span class="s1">&#39;path&#39;</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span>
                            <span class="bp">self</span><span class="o">.</span><span class="n">attachment</span><span class="p">)</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">attachment</span> <span class="k">else</span> <span class="kc">None</span><span class="p">,</span>
                        <span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
                        <span class="s1">&#39;content&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">content</span><span class="p">,</span>
                        <span class="s1">&#39;on_disk&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">on_disk</span>
                    <span class="p">}])</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;Only file attachments can be attached&#39;</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="fm">__repr__</span><span class="p">()</span>

    <span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s1">&#39;Attachment: </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span></div>


<div class="viewcode-block" id="BaseAttachments"><a class="viewcode-back" href="../../../api/attachment.html#O365.utils.attachment.BaseAttachments">[docs]</a><span class="k">class</span> <span class="nc">BaseAttachments</span><span class="p">(</span><span class="n">ApiComponent</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; A Collection of BaseAttachments &quot;&quot;&quot;</span>

    <span class="n">_endpoints</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s1">&#39;attachments&#39;</span><span class="p">:</span> <span class="s1">&#39;/messages/</span><span class="si">{id}</span><span class="s1">/attachments&#39;</span><span class="p">,</span>
        <span class="s1">&#39;attachment&#39;</span><span class="p">:</span> <span class="s1">&#39;/messages/</span><span class="si">{id}</span><span class="s1">/attachments/</span><span class="si">{ida}</span><span class="s1">&#39;</span>
    <span class="p">}</span>
    <span class="n">_attachment_constructor</span> <span class="o">=</span> <span class="n">BaseAttachment</span>

<div class="viewcode-block" id="BaseAttachments.__init__"><a class="viewcode-back" href="../../../api/attachment.html#O365.utils.attachment.BaseAttachments.__init__">[docs]</a>    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parent</span><span class="p">,</span> <span class="n">attachments</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; Attachments must be a list of path strings or dictionary elements</span>

<span class="sd">        :param Account parent: parent object</span>
<span class="sd">        :param attachments: list of attachments</span>
<span class="sd">        :type attachments: list[str] or list[Path] or str or Path or dict</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">protocol</span><span class="o">=</span><span class="n">parent</span><span class="o">.</span><span class="n">protocol</span><span class="p">,</span>
                         <span class="n">main_resource</span><span class="o">=</span><span class="n">parent</span><span class="o">.</span><span class="n">main_resource</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_parent</span> <span class="o">=</span> <span class="n">parent</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__attachments</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="c1"># holds on_cloud attachments removed from the parent object</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__removed_attachments</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">untrack</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">if</span> <span class="n">attachments</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">attachments</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">untrack</span> <span class="o">=</span> <span class="kc">False</span></div>

    <span class="k">def</span> <span class="fm">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">iter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__attachments</span><span class="p">)</span>

    <span class="k">def</span> <span class="fm">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__attachments</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>

    <span class="k">def</span> <span class="fm">__contains__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">item</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">item</span> <span class="ow">in</span> <span class="p">{</span><span class="n">attachment</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">attachment</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">__attachments</span><span class="p">}</span>

    <span class="k">def</span> <span class="fm">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__attachments</span><span class="p">)</span>

    <span class="k">def</span> <span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">attachments</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__attachments</span><span class="p">)</span>
        <span class="n">parent_has_attachments</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_parent</span><span class="p">,</span> <span class="s1">&#39;has_attachments&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">parent_has_attachments</span> <span class="ow">and</span> <span class="n">attachments</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">return</span> <span class="s1">&#39;Number of Attachments: unknown&#39;</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="s1">&#39;Number of Attachments: </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">attachments</span><span class="p">)</span>

    <span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="fm">__str__</span><span class="p">()</span>

    <span class="k">def</span> <span class="fm">__bool__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">bool</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__attachments</span><span class="p">))</span>

<div class="viewcode-block" id="BaseAttachments.to_api_data"><a class="viewcode-back" href="../../../api/attachment.html#O365.utils.attachment.BaseAttachments.to_api_data">[docs]</a>    <span class="k">def</span> <span class="nf">to_api_data</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; Returns a dict to communicate with the server</span>

<span class="sd">        :rtype: dict</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">attachment</span><span class="o">.</span><span class="n">to_api_data</span><span class="p">()</span> <span class="k">for</span> <span class="n">attachment</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">__attachments</span> <span class="k">if</span>
                <span class="n">attachment</span><span class="o">.</span><span class="n">on_cloud</span> <span class="ow">is</span> <span class="kc">False</span><span class="p">]</span></div>

<div class="viewcode-block" id="BaseAttachments.clear"><a class="viewcode-back" href="../../../api/attachment.html#O365.utils.attachment.BaseAttachments.clear">[docs]</a>    <span class="k">def</span> <span class="nf">clear</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; Clear the attachments &quot;&quot;&quot;</span>
        <span class="k">for</span> <span class="n">attachment</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">__attachments</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">attachment</span><span class="o">.</span><span class="n">on_cloud</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">__removed_attachments</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">attachment</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__attachments</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_update_parent_attachments</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_track_changes</span><span class="p">()</span></div>

    <span class="k">def</span> <span class="nf">_track_changes</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; Update the track_changes on the parent to reflect</span>
<span class="sd">        a needed update on this field &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_parent</span><span class="p">,</span> <span class="s1">&#39;_track_changes&#39;</span><span class="p">,</span>
                   <span class="kc">None</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">untrack</span> <span class="ow">is</span> <span class="kc">False</span><span class="p">:</span>
            <span class="c1"># noinspection PyProtectedMember</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_parent</span><span class="o">.</span><span class="n">_track_changes</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="s1">&#39;attachments&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_update_parent_attachments</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; Tries to update the parent property &#39;has_attachments&#39; &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_parent</span><span class="o">.</span><span class="n">has_attachments</span> <span class="o">=</span> <span class="nb">bool</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__attachments</span><span class="p">))</span>
        <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
            <span class="k">pass</span>

<div class="viewcode-block" id="BaseAttachments.add"><a class="viewcode-back" href="../../../api/attachment.html#O365.utils.attachment.BaseAttachments.add">[docs]</a>    <span class="k">def</span> <span class="nf">add</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attachments</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; Add more attachments</span>

<span class="sd">        :param attachments: list of attachments</span>
<span class="sd">        :type attachments: list[str] or list[Path] or str or Path or dict</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">attachments</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">attachments</span><span class="p">,</span> <span class="p">(</span><span class="nb">str</span><span class="p">,</span> <span class="n">Path</span><span class="p">)):</span>
                <span class="n">attachments</span> <span class="o">=</span> <span class="p">[</span><span class="n">attachments</span><span class="p">]</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">attachments</span><span class="p">,</span> <span class="p">(</span><span class="nb">list</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">,</span> <span class="nb">set</span><span class="p">)):</span>
                <span class="c1"># User provided attachments</span>
                <span class="n">attachments_temp</span> <span class="o">=</span> <span class="p">[</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_attachment_constructor</span><span class="p">(</span><span class="n">attachment</span><span class="p">,</span> <span class="n">parent</span><span class="o">=</span><span class="bp">self</span><span class="p">)</span>
                    <span class="k">for</span> <span class="n">attachment</span> <span class="ow">in</span> <span class="n">attachments</span><span class="p">]</span>
            <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">attachments</span><span class="p">,</span>
                            <span class="nb">dict</span><span class="p">)</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cloud_data_key</span> <span class="ow">in</span> <span class="n">attachments</span><span class="p">:</span>
                <span class="c1"># Cloud downloaded attachments. We pass on_cloud=True</span>
                <span class="c1"># to track if this attachment is saved on the server</span>
                <span class="n">attachments_temp</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">_attachment_constructor</span><span class="p">(</span>
                    <span class="p">{</span><span class="bp">self</span><span class="o">.</span><span class="n">_cloud_data_key</span><span class="p">:</span> <span class="n">attachment</span><span class="p">},</span> <span class="n">parent</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                    <span class="n">on_cloud</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
                    <span class="k">for</span> <span class="n">attachment</span> <span class="ow">in</span>
                    <span class="n">attachments</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_cloud_data_key</span><span class="p">,</span> <span class="p">[])]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Attachments must be a str or Path or a &#39;</span>
                                 <span class="s1">&#39;list, tuple or set of the former&#39;</span><span class="p">)</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">__attachments</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">attachments_temp</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_update_parent_attachments</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_track_changes</span><span class="p">()</span></div>

<div class="viewcode-block" id="BaseAttachments.remove"><a class="viewcode-back" href="../../../api/attachment.html#O365.utils.attachment.BaseAttachments.remove">[docs]</a>    <span class="k">def</span> <span class="nf">remove</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attachments</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; Remove the specified attachments</span>

<span class="sd">        :param attachments: list of attachments</span>
<span class="sd">        :type attachments: list[str] or list[Path] or str or Path or dict</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">attachments</span><span class="p">,</span> <span class="p">(</span><span class="nb">list</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">)):</span>
            <span class="n">attachments</span> <span class="o">=</span> <span class="p">({</span><span class="n">attachment</span><span class="o">.</span><span class="n">name</span>
                            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">attachment</span><span class="p">,</span> <span class="n">BaseAttachment</span><span class="p">)</span>
                            <span class="k">else</span> <span class="n">attachment</span> <span class="k">for</span> <span class="n">attachment</span> <span class="ow">in</span> <span class="n">attachments</span><span class="p">})</span>
        <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">attachments</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
            <span class="n">attachments</span> <span class="o">=</span> <span class="p">{</span><span class="n">attachments</span><span class="p">}</span>
        <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">attachments</span><span class="p">,</span> <span class="n">BaseAttachment</span><span class="p">):</span>
            <span class="n">attachments</span> <span class="o">=</span> <span class="p">{</span><span class="n">attachments</span><span class="o">.</span><span class="n">name</span><span class="p">}</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Incorrect parameter type for attachments&#39;</span><span class="p">)</span>

        <span class="n">new_attachments</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">attachment</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">__attachments</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">attachment</span><span class="o">.</span><span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">attachments</span><span class="p">:</span>
                <span class="n">new_attachments</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">attachment</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">attachment</span><span class="o">.</span><span class="n">on_cloud</span><span class="p">:</span>
                    <span class="c1"># add to removed_attachments so later we can delete them</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">__removed_attachments</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                        <span class="n">attachment</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__attachments</span> <span class="o">=</span> <span class="n">new_attachments</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_update_parent_attachments</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_track_changes</span><span class="p">()</span></div>

<div class="viewcode-block" id="BaseAttachments.download_attachments"><a class="viewcode-back" href="../../../api/attachment.html#O365.utils.attachment.BaseAttachments.download_attachments">[docs]</a>    <span class="k">def</span> <span class="nf">download_attachments</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; Downloads this message attachments into memory.</span>
<span class="sd">        Need a call to &#39;attachment.save&#39; to save them on disk.</span>

<span class="sd">        :return: Success / Failure</span>
<span class="sd">        :rtype: bool</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_parent</span><span class="o">.</span><span class="n">has_attachments</span><span class="p">:</span>
            <span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span>
                <span class="s1">&#39;Parent </span><span class="si">{}</span><span class="s1"> has no attachments, skipping out early.&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_parent</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">))</span>
            <span class="k">return</span> <span class="kc">False</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_parent</span><span class="o">.</span><span class="n">object_id</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span>
                <span class="s1">&#39;Attempted to download attachments of an unsaved </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_parent</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">))</span>

        <span class="n">url</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">build_url</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_endpoints</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;attachments&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
            <span class="nb">id</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_parent</span><span class="o">.</span><span class="n">object_id</span><span class="p">))</span>

        <span class="n">response</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_parent</span><span class="o">.</span><span class="n">con</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">response</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">False</span>

        <span class="n">attachments</span> <span class="o">=</span> <span class="n">response</span><span class="o">.</span><span class="n">json</span><span class="p">()</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;value&#39;</span><span class="p">,</span> <span class="p">[])</span>

        <span class="c1"># Everything received from cloud must be passed as self._cloud_data_key</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">untrack</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">add</span><span class="p">({</span><span class="bp">self</span><span class="o">.</span><span class="n">_cloud_data_key</span><span class="p">:</span> <span class="n">attachments</span><span class="p">})</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">untrack</span> <span class="o">=</span> <span class="kc">False</span>

        <span class="c1"># TODO: when it&#39;s a item attachment the attachment itself</span>
        <span class="c1">#  is not downloaded. We must download it...</span>
        <span class="c1"># TODO: idea: retrieve the attachments ids&#39; only with</span>
        <span class="c1">#  select and then download one by one.</span>
        <span class="k">return</span> <span class="kc">True</span></div>

    <span class="k">def</span> <span class="nf">_update_attachments_to_cloud</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; Push new, unsaved attachments to the cloud and remove removed</span>
<span class="sd">        attachments. This method should not be called for non draft messages.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">url</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">build_url</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_endpoints</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;attachments&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
            <span class="nb">id</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_parent</span><span class="o">.</span><span class="n">object_id</span><span class="p">))</span>

        <span class="c1"># ! potentially several api requests can be made by this method.</span>

        <span class="k">for</span> <span class="n">attachment</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">__attachments</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">attachment</span><span class="o">.</span><span class="n">on_cloud</span> <span class="ow">is</span> <span class="kc">False</span><span class="p">:</span>
                <span class="c1"># upload attachment:</span>
                <span class="n">response</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_parent</span><span class="o">.</span><span class="n">con</span><span class="o">.</span><span class="n">post</span><span class="p">(</span><span class="n">url</span><span class="p">,</span>
                                                 <span class="n">data</span><span class="o">=</span><span class="n">attachment</span><span class="o">.</span><span class="n">to_api_data</span><span class="p">())</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">response</span><span class="p">:</span>
                    <span class="k">return</span> <span class="kc">False</span>

                <span class="n">data</span> <span class="o">=</span> <span class="n">response</span><span class="o">.</span><span class="n">json</span><span class="p">()</span>

                <span class="c1"># update attachment data</span>
                <span class="n">attachment</span><span class="o">.</span><span class="n">attachment_id</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;id&#39;</span><span class="p">)</span>
                <span class="n">attachment</span><span class="o">.</span><span class="n">content</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_cc</span><span class="p">(</span><span class="s1">&#39;contentBytes&#39;</span><span class="p">),</span> <span class="kc">None</span><span class="p">)</span>
                <span class="n">attachment</span><span class="o">.</span><span class="n">on_cloud</span> <span class="o">=</span> <span class="kc">True</span>

        <span class="k">for</span> <span class="n">attachment</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">__removed_attachments</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">attachment</span><span class="o">.</span><span class="n">on_cloud</span> <span class="ow">and</span> <span class="n">attachment</span><span class="o">.</span><span class="n">attachment_id</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="c1"># delete attachment</span>
                <span class="n">url</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">build_url</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_endpoints</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;attachment&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                    <span class="nb">id</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_parent</span><span class="o">.</span><span class="n">object_id</span><span class="p">,</span> <span class="n">ida</span><span class="o">=</span><span class="n">attachment</span><span class="o">.</span><span class="n">attachment_id</span><span class="p">))</span>

                <span class="n">response</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_parent</span><span class="o">.</span><span class="n">con</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">response</span><span class="p">:</span>
                    <span class="k">return</span> <span class="kc">False</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">__removed_attachments</span> <span class="o">=</span> <span class="p">[]</span>  <span class="c1"># reset the removed attachments</span>

        <span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;Successfully updated attachments on </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_parent</span><span class="o">.</span><span class="n">object_id</span><span class="p">))</span>

        <span class="k">return</span> <span class="kc">True</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, Narcolapser

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script src="../../../_static/jquery.js"></script>
        <script src="../../../_static/underscore.js"></script>
        <script src="../../../_static/doctools.js"></script>
        <script src="../../../_static/language_data.js"></script>
    

  

  <script type="text/javascript" src="../../../_static/js/theme.js"></script>

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>